import 'package:imember/Util/Global.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:imember/Util/MyColor.dart';
import 'package:imember/Util/PageUtil.dart';
import 'package:imember/Widget/MyButton.dart';
import 'package:imember/Widget/MyImage.dart';

class MyDialog extends StatelessWidget {
  Widget dialog;

  MyDialog(this.dialog);

  @override
  Widget build(BuildContext context) {
    Duration insetAnimationDuration = const Duration(milliseconds: 100);
    Curve insetAnimationCurve = Curves.decelerate;
    return AnimatedPadding(
      padding: MediaQuery.of(context).viewInsets + const EdgeInsets.symmetric(horizontal: 24.0, vertical: 24.0),
      duration: insetAnimationDuration,
      curve: insetAnimationCurve,
      child: MediaQuery.removeViewInsets(
        removeLeft: true,
        removeTop: true,
        removeRight: true,
        removeBottom: true,
        context: context,
        child: Center(
          child: dialog,
        ),
      ),
    );
  }
}

class ScaffoldDialog extends StatelessWidget {
  Widget dialog;

  ScaffoldDialog(this.dialog);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // resizeToAvoidBottomInset: false,
      backgroundColor: MyColor.Transfer,
      body: MediaQuery.removeViewInsets(
        removeLeft: true,
        removeTop: true,
        removeRight: true,
        removeBottom: true,
        context: context,
        child: Center(
          child: dialog,
        ),
      ),
    );
  }
}

///只有文字的弹窗
class TextDialog extends StatelessWidget {
  String content;

  TextDialog(this.content);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 295,
      decoration: BoxDecoration(color: MyColor.White, borderRadius: BorderRadius.circular(8)),
      child: Material(
        color: MyColor.Transfer,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
              height: 32,
              decoration: BoxDecoration(color: MyColor.Orange, borderRadius: BorderRadius.vertical(top: Radius.circular(8))),
            ),
            Container(
                padding: EdgeInsets.fromLTRB(30, 23, 30, 30),
                child: Text(content, style: TextStyle(fontSize: 15, color: MyColor.C70, fontWeight: FontWeight.bold)))
          ],
        ),
      ),
    );
  }
}

void showTextDialog(BuildContext context, String content) {
  showWidgetDialog(context, TextDialog(content));
}

/// 單按鈕dialog
class Btn1Dialog extends StatelessWidget {
  String title;
  String btnText;
  Function onTap;

  Btn1Dialog(this.title, this.btnText, this.onTap);

  @override
  Widget build(BuildContext context) {
    return Container(
        width: 295,
        decoration: BoxDecoration(color: MyColor.White, borderRadius: BorderRadius.circular(8)),
        child: Material(
            color: MyColor.Transfer,
            child: Column(mainAxisSize: MainAxisSize.min, children: [
              Container(
                height: 32,
                decoration: BoxDecoration(color: MyColor.Orange, borderRadius: BorderRadius.vertical(top: Radius.circular(8))),
              ),
              Container(
                  padding: EdgeInsets.fromLTRB(30, 23, 30, 0),
                  child: Text(title, style: TextStyle(fontSize: 15, color: MyColor.C70, fontWeight: FontWeight.bold))),
              OrangeBtn(btnText,width: 128,height: 28,fontSize: 15,margin: EdgeInsets.all(20),onTap: (){
                PageUtil().back(context);
                onTap();
              })
            ])));
  }
}

void showBtn1Dialog(BuildContext context, String title, String btnText, Function onTap) {
  showWidgetDialog(context, Btn1Dialog(title, btnText, onTap));
}

/// 雙按鈕dialog
class Btn2Dialog extends StatelessWidget {
  String content;
  String btn1Text;
  Function on1Tap;
  String btn2Text;
  Function on2Tap;

  Btn2Dialog(this.content, this.btn1Text, this.on1Tap, this.btn2Text, this.on2Tap);

  @override
  Widget build(BuildContext context) {
    return Container(
        width: 469,
        child: Column(mainAxisSize: MainAxisSize.min, children: [
          Container(
              height: 52,
              width: double.infinity,
              alignment: Alignment.center,
              decoration: BoxDecoration(color: MyColor.Orange, borderRadius: BorderRadius.vertical(top: Radius.circular(8))),
              child: Text(content, style: TextStyle(fontSize: 20, color: MyColor.White, fontWeight: FontWeight.bold))),
          Container(
              color: MyColor.White,
              padding: EdgeInsets.all(40),
              child: Row(mainAxisAlignment: MainAxisAlignment.spaceAround, children: [
                Button(btn1Text, height: 37, width: 170, radius: 20, bgColor: MyColor.C99, fontColor: MyColor.White, onTap: () {
                  Navigator.pop(context);
                  on1Tap();
                }),
                Button(btn2Text, height: 37, width: 170, radius: 20, bgColor: MyColor.Orange, fontColor: MyColor.White, onTap: () {
                  Navigator.pop(context);
                  on2Tap();
                })
              ]))
        ]));
  }
}

void showBtn2Dialog(BuildContext context, String content, String btn1Text, Function on1Tap, String btn2Text, Function on2Tap) {
  showWidgetDialog(context, Btn2Dialog(content, btn1Text, on1Tap, btn2Text, on2Tap));
}

void showScaffoldBtn2Dialog(
    BuildContext context, String content, String btnLeftText, Function onLeftTap, String btnRightText, Function onRightTap) {
  showScaffoldDialog(context, Btn2Dialog(content, btnLeftText, onLeftTap, btnRightText, onRightTap));
}

/// 自定義弹窗
void showWidgetDialog(BuildContext context, Widget dialog) {
  FocusManager.instance.primaryFocus?.unfocus();
  showDialog(
      context: context,
      barrierDismissible: true,
      builder: (context) {
        return MyDialog(dialog);
      });
}

void showScaffoldDialog(BuildContext context, Widget dialog) {
  FocusManager.instance.primaryFocus?.unfocus();
  showDialog(
      context: context,
      barrierDismissible: true,
      builder: (context) {
        return ScaffoldDialog(dialog);
      });
}

///底部滾輪選擇器
void showWheelDialog(BuildContext context, String title, List<String> menus, Function(int index) onSelected) {
  FocusManager.instance.primaryFocus?.unfocus();
  showModalBottomSheet(
      context: context,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.vertical(top: Radius.circular(10))),
      builder: (context) {
        return wheelView(title, menus, onSelected);
      });
}

class wheelView extends StatelessWidget {
  String title;
  List<String> menus;
  Function(int index) onSelected;
  int selectIndex = 0;

  wheelView(this.title, this.menus, this.onSelected);

  @override
  Widget build(BuildContext context) {
    return Container(
        width: double.infinity,
        child: Column(mainAxisSize: MainAxisSize.min, children: [
          Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
            TextBtn("取消", height: 57, margin: EdgeInsets.fromLTRB(15, 0, 15, 0), onTap: () {
              PageUtil().back(context);
            }),
            Text(title, style: TextStyle(color: MyColor.Black, fontSize: 16, fontWeight: FontWeight.bold)),
            TextBtn("完成", height: 57, margin: EdgeInsets.fromLTRB(15, 0, 15, 0), onTap: () {
              PageUtil().back(context);
              onSelected(selectIndex);
            }),
          ]),
          Divider(height: 1, color: MyColor.E9),
          Container(
              height: 200,
              child: CupertinoPicker.builder(
                selectionOverlay: Column(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
                  Divider(height: 1, color: MyColor.E9, endIndent: 15, indent: 15),
                  Divider(height: 1, color: MyColor.E9, endIndent: 15, indent: 15),
                ]),
                childCount: menus.length,
                diameterRatio: 2,
                squeeze: 1,
                onSelectedItemChanged: (index) {
                  selectIndex = index;
                  print("onSelectedItemChanged:$index");
                },
                itemExtent: 40,
                itemBuilder: (BuildContext context, int index) {
                  return TextBtn(menus[index], height: 40);
                },
              ))
        ]));
  }
}
